package com.dao;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;

public class BaseDao {
	public static List<List> executeQuery(String sql){
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<List> tableList = new ArrayList<List>();
		try {
			conn  =getConnection();
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int colCount = rsmd.getColumnCount();
			for(int i=0; i<colCount; i++){
				String colName = rsmd.getColumnName(i+1);
				System.out.print(colName+"\t");
			}
			System.out.println();
			while(rs.next()){
				List rowList = new ArrayList();
				for(int i=0; i<colCount; i++){
					String colValue = rs.getString(i+1);
					rowList.add(colValue);
				}
				tableList.add(rowList);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, stmt, null);
		}
		
		return tableList;
	
	}
	
	public static int executeUpdate(String sql,Object [] args){
		Connection conn = null;
		PreparedStatement pstmt=null;
		int result  = 0;
		try {
			conn = getConnectionWeb();
			pstmt = conn.prepareStatement(sql);
			if(args != null){
				for(int i=0; i<args.length; i++){
					pstmt.setObject(i+1, args[i]);
				}
			}
			result = pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, pstmt, null);
		}
		
		return result;
	}
	public static Connection getConnectionWeb(){
		Connection conn = null;
		try {
			ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
			
			String username = bundle.getString("username");
			String password = bundle.getString("password");
			String url = bundle.getString("url");
			String driverClass = bundle.getString("driverClass");
			
			Class.forName(driverClass);
			conn = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static Connection getConnection(){
		Connection conn = null;
		try {
			
			InputStream is =BaseDao.class.getClass().getResourceAsStream("/jdbc.properties");
			Properties p = new Properties();
			p.load(is);
			String username = (String) p.get("username");
			String password = (String)p.get("password");
			String url = (String)p.get("url");
			String driverClass = (String)p.get("driverClass");
			
			Class.forName(driverClass);
			conn = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	public static void closeAll(Connection conn,Statement stmt,ResultSet rs){
		try {
			if(rs != null){
				rs.close();
			}
			if(stmt!=null){
				stmt.close();
			}
			if(conn!=null){
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
}
